package com.lujieni.springbootmybatis.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.lujieni.springbootmybatis.interceptor.TenantDatabaseInterceptor;
import com.lujieni.springbootmybatis.permission.handler.CustomDataPermissionHandler;
import com.lujieni.springbootmybatis.permission.rule.CommonPermissionRule;
import com.lujieni.springbootmybatis.permission.rule.CommonPermissionRuleCustomizer;
import com.lujieni.springbootmybatis.po.StudentDO;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.List;

@AutoConfiguration
public class MyBatisPlusConfig {

    /**
     * 多租户字段名(数据库中的租户ID列名)
     */
    private static final String TENANT_COLUMN = "tenant_id";

    @Bean
    public CommonPermissionRule generateCommonPersissonRule(){
        return new CommonPermissionRule();
    }

    /**
     * 先添加的拦截器先执行
     * 在使用多个插件时,请将分页插件放到插件执行链的最后面,以避免 COUNT SQL 执行不准确的问题。
     * @return
     */
    @Bean
    public String wrapMybatisPlusInterceptor(MybatisPlusInterceptor interceptor, List<CommonPermissionRuleCustomizer> customizers, CommonPermissionRule rule) {
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor());
        // 添加多租户插件必须放在第一个
        interceptor.addInnerInterceptor(tenantLineInnerInterceptor);

        customizers.forEach(e->{
            e.customize(rule);
        });

        // DataPermissionInterceptor是mybatis-plus数据权限插件,CustomDataPermissionHandler是自己定义的必须实现MultiDataPermissionHandler
        DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor(new CustomDataPermissionHandler(rule));
        interceptor.addInnerInterceptor(dataPermissionInterceptor);

        // 添加分页插件
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return "hello";
    }

    /**
     * 独立为1个方法,MybatisPlusAutoConfiguration在执行构造器方法的时候会先执行mybatisPlusInterceptor
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        return new MybatisPlusInterceptor();
    }

    @Bean
    public CommonPermissionRuleCustomizer websiteMsgPermissionRuleCustomizer(){
      return new CommonPermissionRuleCustomizer() {
          @Override
          public void customize(CommonPermissionRule rule) {
              rule.addTableName("t_website_msg");
              rule.addTableFieldMap("t_website_msg", "user_code");
          }
      };
    }

    @Bean
    public CommonPermissionRuleCustomizer studentPermissionRuleCustomizer(){
        return new CommonPermissionRuleCustomizer() {
            @Override
            public void customize(CommonPermissionRule rule) {
                rule.addDataColumn(StudentDO.class, "id");
            }
        };
    }



}